package com.hui;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.item.*;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.security.PublicKey;
import java.util.Arrays;
import java.util.List;


/**
 * @author zhang zhongtao
 */
@Configuration
@Slf4j
public class SpringBatchLearn10Configuration {
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    private static final String jobName = "springbatch-learn-10-jobListener";

    @Bean
    public Job myListenerJob() {
        return jobBuilderFactory.get("myListenerJob")
                .listener(new MyJobListener())
                .start(myListenerStep1())
                .build();
    }

    @Bean
    public Step myListenerStep1() {
        return stepBuilderFactory.get("myListenerStep1")
                .<String,String>chunk(2)
                .faultTolerant()
                .listener(new MyChunkListener())
                .reader(reader())
                .writer(writer())
                .build();
    }

    @Bean
    public ItemWriter<? super String> writer() {
        return new ItemWriter<String>() {
            @Override
            public void write(List<? extends String> list) throws Exception {
                for (String s : list) {
                    log.info("\n\rBatchConfiguration.writer{}", s);
                }
            }
        };
    }

    @Bean
    public ItemReader<String> reader() {
        return new ListItemReader<String>(Arrays.asList("1", "2", "3"));
    }

}
